﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="NullCacheProviderTests.cs" company="XamlNinja">
//   2011 Richard Griffin and Ollie Riches
// </copyright>
// <summary>
//   Null cache provider unit tests - we follow BDD principles, http://dannorth.net/introducing-bdd/
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WP7Contrib.Caching.Tests
{
    using System;
    using Microsoft.Silverlight.Testing;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Null cache provider unit tests - we follow BDD principles, http://dannorth.net/introducing-bdd/
    /// </summary>
    [TestClass]
    public class NullCacheProviderTests : SilverlightTest
    {
        [TestMethod]
        public void ShouldCreatedAnInstanceWithoutSideAffects()
        {
            // Given and When we create an instance of the NullCacheProviderTests provider...
            var cacheProvider = new NullCacheProvider();

            // Then we don't expect any side effect to be generated...
            Assert.IsNotNull(cacheProvider);
        }

        [TestMethod]
        public void ShouldAddAValueWithoutSideAffects()
        {
            // Given we can create an instance...
            var cacheProvider = new NullCacheProvider();

            // Given we have a value to add to the cache...
            var value = new CacheValue<int>(42);

            // When we add value to the cache...
            cacheProvider.Add("MyKey", value, DateTime.Now.AddDays(1));

            // Then we don't expect an exception to be thrown
        }

        [TestMethod]
        public void ShouldAddANullValueWithoutSideAffects()
        {
            // Given we can create an instance...
            var cacheProvider = new NullCacheProvider();

            // When we add a null value from the cache...
            cacheProvider.Add<int, CacheValue<int>>(12, null, DateTime.Now.AddDays(1));

            // Then we don't expect an exception to be thrown
        }

        [TestMethod]
        public void ShouldNotThrowExceptionWhenKeyIsNull()
        {
            // Given we can create an instance...
            var cacheProvider = new NullCacheProvider();

            // Given we have a value to add to the cache...
            var value = new CacheValue<int>(42);

            // When we add a null value to the cache...
            cacheProvider.Add<string, CacheValue<int>>(null, value, DateTime.Now.AddDays(1));

            // Then we don't expect an exception to be thrown
        }

        [TestMethod]
        public void ShouldRemoveAValueWithoutSideAffects()
        {
            // Given we can create an instance...
            var cacheProvider = new NullCacheProvider();

            // When we remove a value from the cache...
            cacheProvider.Remove("MyKey");

            // Then we don't expect an exception to be thrown
        }

        [TestMethod]
        public void ShouldRemoveANullKeyWithoutSideAffects()
        {
            // Given we can create an instance...
            var cacheProvider = new NullCacheProvider();

            // When we remove a value using a null key from the cache...
            cacheProvider.Remove<string>(null);

            // Then we don't expect an exception to be thrown
        }

        [TestMethod]
        public void ShouldClearWithoutSideAffects()
        {
            // Given we can create an instance...
            var cacheProvider = new NullCacheProvider();

            // When we clear the cache...
            cacheProvider.Clear();

            // Then we don't expect an exception to be thrown
        }

        [TestMethod]
        public void ShouldReturnNullWhenReadingFromCache()
        {
            // Given we can create an instance of the null cache provider...
            var cacheProvider = new NullCacheProvider();

            // Given we have a value to add to the cache...
            var value = new CacheValue<int>(42);

            // Given we add an item to the cache...
            cacheProvider.Add("MyKey", value, DateTime.Now.AddDays(1));

            // When we get the value from the cache...
            var cachedValue = cacheProvider.Get<string, CacheValue<int>>("MyKey");

            // Then we except the value to be null...
            Assert.IsNull(cachedValue);
        }
    }
}
